<?php
/**
 * 系统配置服务，默认数据在用代码进行配置
 * 动态修改的数据放在mongodb中，因为mongodb配置无需预先设置表的结构
 */
namespace globalunit\utils;
use QYS\Db\Mongo;
use QYS\Log\Log;

function merge($dest,$src)
{
    if(!isset($dest))
    {
        $src=array();
    }

}

function  array_merge_recursive_distinct(array $array1, array &$array2)
{
    $merged = $array1;
    foreach ($array2 as $key => &$value) {
        if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
            $merged[$key] = array_merge_recursive_distinct($merged[$key], $value);
        } else {
            $merged[$key] = $value;
        }
    }
    return $merged;
}

class Config
{
    static $DEFAULT=array(
        "errmsg"=>array(
            "e101" => '活动未开始',
            "e102" => '活动已结束',
            "e401" => '认证失败,请稍后再试',
            "e10001" => '参数错误',
            "e10002" => '验证失败，请稍后重试',
            "e10003" => '验证码已发送,请稍后再试',
            "e10004" => '验证码发送失败,请稍后再试',
            "e10005" => '验证码错误',
            "e10006" => '系统繁忙,请稍后再试',
            "e10007" => '用户信息获取失败',
            "e10008" => '创建角色失败,请稍后再试',
            "e10009" => '操作失败,请稍后再试',
            "e10010" => '今日已领取过该星星了',
            "e10011" => '今日已抽过奖了',
            "e10012" => '星星数量不足',
            "e10013" => '请优先创建活动',
            // 管理后台
            "e20000" => '获取后缀失败',
        ),
        "app"=>array(
            "appid" => 'appid-123-123-1234',
            "uuid" => 'uuid-99324957-47df-da92-eeef-894a092e3094',
        ),
    );


    static $DBCONFIG=array();

    public static function loadconfig()
    {
        $conn = Mongo::getInstance('mongo1');
        $gamename=$GLOBALS['GAMENAME'];
        $db=$conn->$gamename;
        $collection=$db->gameconfig;

        foreach (self::$DEFAULT as $key => $value) {
            $cursor = $collection->findOne(["_id"=>$key]);
            if(isset($cursor)){
                $tmp=[];
                foreach($cursor as $key1=>$value1){
                    if($key1!="_id"){
                        $tmp[$key1]=$value1;
                    }
                }
                self::$DBCONFIG[$key]=$tmp;
            }
        }
    }

    public static function getallconfig()
    {
        global $DEFAULT,$DBCONFIG;
        $a1=self::$DEFAULT;
        $a2=self::$DBCONFIG;
        $a3=array_merge_recursive_distinct($a1,$a2);
        return $a3;
    }

    public static function get()
    {
        $d=self::$DBCONFIG;
        foreach (func_get_args() as $v) {
            $k=$v;
            if(!isset($d[$k])){
                $d=null;
                break;
            }
            $d=$d[$k];
        }

        if(isset($d)){
            return $d;
        }

        $d=self::$DEFAULT;

        foreach (func_get_args() as $v) {
            $k=$v;
            if(!isset($d[$k])){
                $d=null;
                break;
            }
            $d=$d[$k];
        }
        assert(isset($d),"没有找到相关的配置".func_get_args()[0]);
        return $d;
    }

    public static function getdefault()
    {
        return self::$DEFAULT;
    }

    public static function getdbconfig()
    {
        return self::$DBCONFIG;
    }
}




